﻿using System;
using System.Web;
using System.Collections.Generic;
using Model; 
using System.Text;
using System.Configuration;
using System.Web.Script.Serialization; 

/// <summary>
/// AlipayUtil 的摘要说明
/// </summary>
public class AlipayUtil
{
    private static readonly AlipayUtil Instance = new AlipayUtil();

    private AlipayUtil()
    {
        
    }

    public static AlipayUtil GetInstance
    {
        get { return Instance; }
    }

    public static SortedDictionary<string, string> GetSParaTemp()
    {
        SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
        sParaTemp.Add("service", Config.service);
        sParaTemp.Add("partner", Config.partner);
        sParaTemp.Add("seller_id", Config.seller_id);
        sParaTemp.Add("_input_charset", Config.input_charset.ToLower());
        sParaTemp.Add("payment_type", Config.payment_type);
        sParaTemp.Add("notify_url", Config.notify_url);
        sParaTemp.Add("anti_phishing_key", Config.anti_phishing_key);
        sParaTemp.Add("exter_invoke_ip", Config.exter_invoke_ip);
        return sParaTemp;
    }

    public static Dictionary<string, string> GetBiz_content()
    {
        Dictionary<string, string> biz_content = new Dictionary<string, string>();
        biz_content.Add("timeout_express", "30m");
        biz_content.Add("seller_id", "2088521485855960");
        biz_content.Add("product_code", "QUICK_MSECURITY_PAY");
        return biz_content;
    }

    public static Dictionary<string, string> GetSparam()
    {
        Dictionary<string, string> sparam = new Dictionary<string, string>();
        sparam.Add("notify_url", Config.appnotify_url); 
        sparam.Add("app_id", "2017020905579031");
        sparam.Add("charset", "utf-8");
        sparam.Add("method", "alipay.trade.app.pay");
        sparam.Add("sign_type", "RSA");
        sparam.Add("version", "1.0");
        sparam.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        return sparam;
    }

    public static string buildOrderParam(Dictionary<string, string> map)
    {

        List<string> keys = new List<string>(map.Keys);

        StringBuilder sb = new StringBuilder();
        //foreach (string s in map.Keys)
        //{
        //    sb.Append(buildKeyValue(s, map[s], true)+"&");
        //}
        for (int i = 0; i < keys.Count - 1; i++)
        {
            string key = keys[i];
            string value = map[key];
            sb.Append(buildKeyValue(key, value, true));
            sb.Append("&");
        }

        string tailKey = keys[keys.Count - 1];
        string tailValue = map[tailKey];
        sb.Append(buildKeyValue(tailKey, tailValue, true));

        return sb.ToString();
    }

    public static string buildKeyValue(string key, string value, bool isEncode)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(key);
        sb.Append("=");
        if (isEncode)
        {
            try
            {
                string str_utf8 = HttpUtility.UrlEncode(value, System.Text.Encoding.UTF8);
                sb.Append(str_utf8);
            }
            catch (Exception)
            {
                sb.Append(value);
            }
        }
        else
        {
            sb.Append(value);
        }
        return sb.ToString();
    }
}